home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 52 / Mac Magazin and MacEasy Magazine CD - Issue 52.iso / Updates / Stata 5.0 Ado-files / ado.sea / newado / stack.ado < prev    next >
Text File  |  1998-11-22  |  5KB  |  228 lines

  1. *! version 3.1.6  09apr1998
  2. program define stack
  3.     version 5.0
  4.     local varlist "req ex min(2)"
  5.     local if "opt"
  6.     local in "opt" 
  7.     local options "Into(str) Group(integer 0) CLEAR WIde"
  8.     parse "`*'"
  9.     if ("`into'"=="") { 
  10.         if `group'==0 { 
  11.             error 198
  12.         }
  13.         Makeinto `group' `varlist'
  14.     }
  15.     else {
  16.         if `group'!=0 { 
  17.             di in red "may not specify both group() and into()"
  18.             exit 198
  19.         }
  20.         Fixinto `into'
  21.     }
  22.     local into $S_1
  23.     global S_1
  24.  
  25.                     /* # of args in varlist */
  26.     local vcnt : word count `varlist'
  27.                     /* # of args in into()    */
  28.     local icnt : word count `into'
  29.  
  30.     local grps `vcnt'/`icnt'    /* # of groups        */
  31.     if (`grps' != int(`grps') | `grps'<=1) {
  32.         di in red "incorrect number of variables specified"
  33.         exit 198
  34.     }
  35.  
  36. /*
  37. Removed by wwg 19sep1997-- Stata 5 automatically repartitions memory
  38.             /* verify whether the result will fit    */
  39.     quietly describe, short
  40.     local maxobs = _result(4)    /* size of partition    */
  41.     capture count `if' `in' 
  42.     local newobs = _result(1)*`grps'    /* # of new obs    */
  43.     if (`newobs' > `maxobs'-20) { 
  44.         di in red "insufficient memory (observations)"
  45.         di in red "`newobs' required and " `maxobs'-20 " available."
  46.         exit 901
  47.     }
  48. */
  49.             /* present warning            */
  50.     if "`clear'"=="" {
  51.         di in blu "Warning:  data in memory will be lost." /*
  52.             */ _n _col(10) /* 
  53.             */ "Press any key to continue, Ctrl-Break to abort."
  54.         set more 0 
  55.         more 
  56.     }
  57.  
  58.     tempfile stacktp
  59.             /* save data needed to stack        */
  60.     if ("`if'"!="" | "`in'"!="") {
  61.         quietly keep `if' `in'
  62.     }
  63.     keep `varlist' 
  64.     quietly save "`stacktp'", replace
  65.  
  66.             /* preserve varlist for wide option    */
  67.     local vlist "`varlist'"
  68.  
  69.             /* stack the data            */
  70.     local grp "1"
  71.     capture {
  72.         while (`grp' <= `grps') { 
  73.             parse "`varlist'", parse(" ")
  74.             local i "1"
  75.             local touse 
  76.             while (`i' <= `icnt') { 
  77.                 local touse "`touse' `1'"
  78.                 mac shift
  79.                 local i = `i' + 1 
  80.             }
  81.             local varlist "`*'"    /* reset varlist    */
  82.             quietly use "`stacktp'", clear
  83.             keep `touse' 
  84.             gen int _stack = `grp'
  85.             local i "1"
  86.             while (`i' <= `icnt') { 
  87.                 local name1 : word `i' of `touse'
  88.                 local new1  : word `i' of `into'
  89.                 if ("`name1'" != "`new1'") {
  90.                     rename `name1' `new1'
  91.                 }
  92.                 local i = `i' + 1 
  93.             }
  94.             tempfile tempfi
  95.             local tf`grp' "`tempfi'"
  96.             quietly save "`tempfi'"
  97.             local grp = `grp' + 1 
  98.         }
  99.  
  100.         quietly use "`tf1'", clear
  101.         local grp 2
  102.         while (`grp' <= `grps') { 
  103.             quietly append using "`tf`grp''"
  104.             local grp = `grp' + 1 
  105.         }                /* done        */
  106.         if ("`wide'"!="") {         /* wide option    */
  107.             local grp 1
  108.             while (`grp' <= `grps') { 
  109.                 parse "`vlist'", parse(" ")
  110.                 local i "1"
  111.                 local touse
  112.                 while (`i' <= `icnt') { 
  113.                     local touse "`touse' `1'"
  114.                     mac shift 
  115.                     local i = `i' + 1 
  116.                 }
  117.                 local vlist "`*'"
  118.                 local i "1"
  119.                 while (`i' <= `icnt') { 
  120.                     local name1 : word `i' of `touse'
  121.                     capture confirm new variable `name1'
  122.                     if (_rc==0) {
  123.                         local name2 : word `i' of `into'
  124.                         local type : type `name2'
  125.                         quietly gen `type' `name1' = /*
  126.                             */ `name2' /*
  127.                             */ if _stack==`grp'
  128.                     }
  129.                     local i = `i' + 1 
  130.                 }
  131.                 local grp = `grp' + 1
  132.             }
  133.         }
  134.         global S_FN
  135.         global S_FNDATE
  136.         exit
  137.     }
  138.                 /* error-abort code    */
  139.     if (_rc) { 
  140.         local rc = _rc 
  141.         drop _all
  142.         di in red "(memory cleared)"
  143.         error `rc'
  144.     }
  145.     global S_FN
  146.     global S_FNDATE
  147. end
  148.  
  149.  
  150. program define Fixinto /* newvarlist */
  151.     parse "`*'", parse("- ") 
  152.     while "`1'" != "" { 
  153.         if "`2'"=="-" { 
  154.             Split "`1'" "`1' `2' `3'"
  155.             local stub "$S_1"
  156.             local n1 $S_2
  157.             Split "`3'" "`1' `2' `3'"
  158.             local n2 "$S_2"
  159.             if "`stub'" != "$S_1" | `n1'>`n2' {
  160.                 Fixerr "`1' `2' `3'"
  161.                 /*NOTREACHED*/
  162.             }
  163.             local i `n1'
  164.             while `i' <= `n2' { 
  165.                 local res "`res' `stub'`i'"
  166.                 local i = `i' + 1
  167.             }
  168.             mac shift 3
  169.         }
  170.         else {
  171.             local res "`res' `1'"
  172.             mac shift 
  173.         }
  174.     }
  175.     global S_2
  176.     global S_1 "`res'"
  177. end
  178.  
  179.  
  180. program define Split /* "varname#" "string for error message" */
  181.     if "`1'"=="" { 
  182.         Fixerr "`2'"
  183.         /*NOTREACHED*/
  184.     }
  185.     local l = length("`1'")
  186.     while index("0123456789",substr("`1'",`l',1)) {
  187.         local l = `l' - 1 
  188.         if `l' == 0 { 
  189.             Fixerr "`2'"
  190.             /*NOTREACHED*/
  191.         }
  192.     }
  193.     global S_1 = substr("`1'",1,`l')
  194.     global S_2 = substr("`1'",`l'+1,.)
  195.     if "$S_1"=="" | "$S_2"=="" { 
  196.         Fixerr "`2'"
  197.         /*NOTREACHED*/
  198.     }
  199. end
  200.  
  201. program define Fixerr
  202.     di in red _quote "`1'" _quote " invalid"
  203.     exit 198
  204. end
  205.  
  206. program define Makeinto /* # varnames */
  207.     local g "`1'"
  208.     if `g' <= 0 { 
  209.         di in red "group(`g') invalid"
  210.         exit 198
  211.     }
  212.     mac shift 
  213.     local n : word count `*'
  214.     local v = `n' / `g'
  215.     if `v' != int(`v') | `v'==0 { 
  216.         di in red "`n' variables cannot be stacked into `g' groups;"
  217.         di in red "there would be " `v' " variables/group."
  218.         exit 198
  219.     }
  220.     local i 1
  221.     global S_1
  222.     while `i' <= `v' { 
  223.         global S_1 $S_1 ``i''
  224.         local i = `i' + 1
  225.     }
  226. end
  227. exit
  228.